﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WFNetLib.Log;

namespace RobotControl
{
    public partial class Form_磁力仪测试 : Form
    {
        int S,M;        
        private bool ComListening = false;//是否没有执行完invoke相关操作              
        private bool ComClosing = false;//是否正在关闭串口，执行Application.DoEvents， 并阻止再次invoke 
        public Form_磁力仪测试(int s,int m)
        {
            InitializeComponent();
            S=s;
            M=m;
            for (int j = 0; j < m; j++)
            {
                listView1.Columns.Add("数据" + (j + 1).ToString());
            }
            listView1.Columns.Add("平均");
            listView1.Columns.Add("除50");
            for(int i=0;i<s;i++)
            {
                ListViewItem l=listView1.Items.Add("传感器"+(i+1).ToString());
                l.Group = listView1.Groups[0];
                for(int j=0;j<m;j++)
                {
                    l.SubItems.Add((j + 1).ToString());                    
                }
                l.SubItems.Add("平均");
                l.SubItems.Add("50");
            }
            for (int i = 0; i < s; i++)
            {
                ListViewItem l = listView1.Items.Add("传感器" + (i + 1).ToString());
                l.Group = listView1.Groups[1];
                for (int j = 0; j < m; j++)
                {
                    l.SubItems.Add((j + 1).ToString());
                }
                l.SubItems.Add("平均");
            }
            for (int i = 0; i < s; i++)
            {
                ListViewItem l = listView1.Items.Add("传感器" + (i + 1).ToString());
                l.Group = listView1.Groups[2];
                for (int j = 0; j < m; j++)
                {
                    l.SubItems.Add((j + 1).ToString());
                }
                l.SubItems.Add("平均");
            }
            cly = new 磁力仪(S, M);
        }

        private void Form_磁力仪测试_Load(object sender, EventArgs e)
        {
            try 
            {
                serialPort1.PortName = Properties.Settings.Default.磁力计通道;
                serialPort1.Open(); 
            }
            catch { MessageBox.Show("磁力仪对应串口无法打开，无法测试磁力仪纳!"); this.Close(); return; }
        }
        磁力仪 cly;
        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            if (ComClosing)
                return;
            ComListening = true;
            byte rx;
            while (serialPort1.BytesToRead != 0)
            {
                int irx = serialPort1.ReadByte();
                if (irx == -1)
                    break;
                rx = (byte)irx;
                if (cly.RxProc(rx))
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        StringBuilder log = new StringBuilder();
//                         for (int i = 0; i < cly.NeedRx; i++)
//                         {
//                             log.Append(cly.RxList[i].ToString("X2") + " ");
//                         }
//                         log.Append("\r\n");                        
                        for (int i = 0; i < S; i++)
                        {
                            ListViewItem l = listView1.Items[i];
                            long arv = 0;
                            log.Append("传感器" + (i + 1).ToString()+"\r\n");
                            for (int j = 0; j < M; j++)
                            {
                                arv += cly.Uncompensated[i, j].ofs_32;
                                l.SubItems[j + 1].Text = (cly.Uncompensated[i, j].ofs_32 *0.0002).ToString();
                                log.Append("数据" + (j + 1).ToString() + ":" + l.SubItems[j + 1].Text + "\r\n");
                            }
                            l.SubItems[M + 1].Text = (arv / M).ToString();
                            l.SubItems[M + 2].Text = (arv / M / 50).ToString();
                            log.Append("平均 :" + l.SubItems[M + 2].Text + "\r\n");
                        }
                        //log.Append("\r\n");
                        //log.Append("\r\n");
                        for (int i = 0; i < S; i++)
                        {
                            //log.Append("传感器" + (i + 1).ToString() + "\r\n");
                            ListViewItem l = listView1.Items[S + i];
                            int arv = 0;
                            for (int j = 0; j < M; j++)
                            {
                                arv += cly.FirstFourADChannels[i, j].ofs_16;
                                l.SubItems[j + 1].Text = cly.FirstFourADChannels[i, j].ofs_16.ToString();
                                //log.Append("数据" + (j + 1).ToString() + ":" + l.SubItems[j + 1].Text + "\r\n");
                            }
                            l.SubItems[M + 1].Text = (arv / M).ToString();
                            //log.Append("平均 :" + l.SubItems[M + 1].Text + "\r\n");
                        }
                        //log.Append("\r\n");
                        //log.Append("\r\n");
                        for (int i = 0; i < S; i++)
                        {
                            ListViewItem l = listView1.Items[S + S + i];
                            //log.Append("传感器" + (i + 1).ToString() + "\r\n");
                            int arv = 0;
                            for (int j = 0; j < M; j++)
                            {
                                arv += cly.RestOfADChannels[i, j].ofs_16;
                                l.SubItems[j + 1].Text = cly.RestOfADChannels[i, j].ofs_16.ToString();
                                //log.Append("数据" + (j + 1).ToString() + ":" + l.SubItems[j + 1].Text + "\r\n");
                            }
                            l.SubItems[M + 1].Text = (arv / M).ToString();
                            //log.Append("平均 :" + l.SubItems[M + 1].Text + "\r\n");
                        }
                        toolStripStatusLabel1.Text = cly.SensorLevel[0].ToString();
                        toolStripStatusLabel2.Text = cly.SensorLevel[1].ToString();
                        toolStripStatusLabel3.Text = cly.SensorLevel[2].ToString();
                        toolStripStatusLabel4.Text = cly.SensorLevel[3].ToString();
                        toolStripStatusLabel5.Text = cly.SynchStatus.ofs_16.ToString();
                        toolStripStatusLabel6.Text = cly.AD8Value.ofs_16.ToString();
                        TextLog.AddTextLog(log.ToString());
                    }
                    ));
                }
            }
            ComListening = false;
        }

        private void Form_磁力仪测试_FormClosing(object sender, FormClosingEventArgs e)
        {
            ComClosing = true;
            while (ComListening)
                Application.DoEvents();
            serialPort1.Close();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            
        }
//         private delegate void delegateChangeParamList();
//         private void ChangeParamList()
//         {
//             for (int i = 0; i < S; i++)
//             {
//                 ListViewItem l = listView1.Items[i];
//                 long arv = 0;
//                 for (int j = 0; j < M; j++)
//                 {
//                     arv += cly.Uncompensated[i, j].ofs_32;
//                     l.SubItems[j + 1].Text = (cly.Uncompensated[i, j].ofs_32 / 50).ToString();
//                 }
//                 l.SubItems[M + 1].Text = (arv / M).ToString();
//                 l.SubItems[M + 2].Text = (arv / M / 50).ToString();
//             }
//             for (int i = 0; i < S; i++)
//             {
//                 ListViewItem l = listView1.Items[S + i];
//                 int arv = 0;
//                 for (int j = 0; j < M; j++)
//                 {
//                     arv += cly.FirstFourADChannels[i, j].ofs_16;
//                     l.SubItems[j + 1].Text = cly.FirstFourADChannels[i, j].ofs_16.ToString();
//                 }
//                 l.SubItems[M + 1].Text = (arv / M).ToString();
//             }
//             for (int i = 0; i < S; i++)
//             {
//                 ListViewItem l = listView1.Items[S + S + i];
//                 int arv = 0;
//                 for (int j = 0; j < M; j++)
//                 {
//                     arv += cly.RestOfADChannels[i, j].ofs_16;
//                     l.SubItems[j + 1].Text = cly.RestOfADChannels[i, j].ofs_16.ToString();
//                 }
//                 l.SubItems[M + 1].Text = (arv / M).ToString();
//             }
//             toolStripStatusLabel1.Text = cly.SensorLevel[0].ToString();
//             toolStripStatusLabel2.Text = cly.SensorLevel[1].ToString();
//             toolStripStatusLabel3.Text = cly.SensorLevel[2].ToString();
//             toolStripStatusLabel4.Text = cly.SensorLevel[3].ToString();
//             toolStripStatusLabel5.Text = cly.SynchStatus.ofs_16.ToString();
//             toolStripStatusLabel6.Text = cly.AD8Value.ofs_16.ToString();
//         }
//         public void ExternChangeParamList()
//         {
//             this.Invoke(new delegateChangeParamList(ChangeParamList));
//         }
    }
}
